<?php
include('db.php');
$db = getDb();

$content="";


if($_GET["a"] == "getGroups")
{
	$sqlCommand="SELECT id,name FROM groups";
	$result = $db->Execute($sqlCommand);
	
	if ($result === false)
		die($sqlCommand);
	
	while (!$result->EOF)
	{
		$content.= "<option value=\"" . $result->fields[0] .
		"\">" . $result->fields[1] . "</option>\n";
	
		$result->MoveNext();
	}
	
}

if($_GET["a"] == "addGroup")
{
	$sqlCommand="INSERT INTO groups(name) values('".
			$_GET["name"] . "') ";
	$result = $db->Execute($sqlCommand);

	if ($result === false)
		die($sqlCommand);
}


if($_GET["a"] == "addEmail")
{
	$sqlCommand="INSERT INTO email(name,address) values('".
	$_GET["name"] . "','". $_GET["address"] ."') ";
	$result = $db->Execute($sqlCommand);
	 
	if ($result === false)
		die($sqlCommand);
}

if($_GET["a"] == "addMember")
{
	$sqlCommand="INSERT INTO emailgroup(emailid,groupid) values('".
			$_GET["eid"] . "','". $_GET["gid"] ."') ";
	$result = $db->Execute($sqlCommand);

	if ($result === false)
		die($sqlCommand);
}

if($_GET["a"] == "removeEmail")
{
	$sqlCommand="DELETE FROM email WHERE id=". $_GET["id"];
	$result = $db->Execute($sqlCommand);

	if ($result === false)
		die($sqlCommand);
}
if($_GET["a"] == "removeMember")
{
	$sqlCommand="DELETE FROM emailgroup WHERE id=". $_GET["id"];
	$result = $db->Execute($sqlCommand);

	if ($result === false)
		die($sqlCommand);
}
if($_GET["a"] == "removeGroup")
{
	$sqlCommand="DELETE FROM groups WHERE id=". $_GET["id"];
	$result = $db->Execute($sqlCommand);

	if ($result === false)
		die($sqlCommand);
}





if($_GET["a"] == "getEmails")
{
	$sqlCommand="SELECT id,address,name FROM email";
	$result = $db->Execute($sqlCommand);

	if ($result === false)
		die($sqlCommand);

	while (!$result->EOF)
	{
		$content.= "<option value=\"" . $result->fields[0] .
		"\">" . $result->fields[1]  . "(". $result->fields[2]  .")</option>\n";
			
		$result->MoveNext();
	}

}

if($_GET["a"] == "getMembers")
{
	$sqlCommand="SELECT eg.id, e.address, e.name FROM email e JOIN emailgroup eg ON e.id = eg.emailId JOIN groups g ON eg.groupId = g.id WHERE g.id = ". $_GET["id"] ."";
	$result = $db->Execute($sqlCommand);

	if ($result === false)
		die($sqlCommand);

	while (!$result->EOF)
	{
		$content.= "<option value=\"" . $result->fields[0] .
		"\">" . $result->fields[1]  . "(". $result->fields[2]  .")</option>\n";
			
		$result->MoveNext();
	}

}
	

echo $content;

?>